package com.tqw.shipping.service.imp;

import com.tqw.entity.Result;
import com.tqw.shipping.dto.ShippingDTO;
import com.tqw.shipping.mapper.ShippingMapper;
import com.tqw.shipping.pojo.Area;
import com.tqw.shipping.pojo.Receiving;
import com.tqw.shipping.service.AreaService;
import com.tqw.shipping.service.ShippingService;
import com.tqw.vo.AreaVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: czm
 * @Date: 2022/04/12/19:35
 * @Description:
 */
@Service
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    @Autowired
    private AreaService areaService;

    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public Result<List<ShippingDTO>> list(Integer clientId) {
        List<Receiving> list = shippingMapper.list(clientId);
        List<ShippingDTO> shippingDTOList = new ArrayList<>();
        for (int i = 0;i<list.size();i++){
            ShippingDTO shippingDTO = new ShippingDTO();
            AreaVO areaVO =  areaService.getAreaInfo(list.get(i).getStreetId());
            shippingDTO.setAddressId(list.get(i).getAddressId()).setConsignee(list.get(i).getConsignee())
                    .setAddressDetail(list.get(i).getAddressDetail())
                    .setPhone(list.get(i).getPhone()).setIsDefault(list.get(i).getIsDefault())
                    .setProvinceId(areaVO.getProvinceId()).setProvinceName(areaVO.getProvinceName())
                    .setCityId(areaVO.getCityId()).setCityName(areaVO.getCityName())
                    .setRegionId(areaVO.getRegionId()).setRegionName(areaVO.getRegionName())
                    .setStreetId(areaVO.getStreetId()).setStreetName(areaVO.getStreetName());
            shippingDTOList.add(shippingDTO);
        }
        Result<List<ShippingDTO>> result = new Result<>();
        result.setData(shippingDTOList);
        return result;
    }

    @Override
    @Transactional
    public void save(Receiving receiving) {
        if (receiving.getIsDefault().equals(Receiving.DEFAULT_ADDR)){
            shippingMapper.removeDefaultAddr(receiving.getClientId());
        }
        shippingMapper.save(receiving);
    }

    @Override
    @Transactional
    public void update(Receiving receiving) {
        if (receiving.getIsDefault().equals(Receiving.DEFAULT_ADDR)){
            shippingMapper.removeDefaultAddr(receiving.getClientId());
        }
        shippingMapper.update(receiving);
    }

    @Override
    public int countByUserId(Integer clientId) {
        return shippingMapper.countByClientId(clientId);
    }

    @Override
    public ShippingDTO getAddrByClientIdAndId(Integer addressId, int clientId) {
        Receiving addr = shippingMapper.getAddrByClientIdAndId(addressId, clientId);
        AreaVO areaInfo = areaService.getAreaInfo(addr.getStreetId());
        ShippingDTO shippingDTO = new ShippingDTO();
        shippingDTO.setAddressDetail(addr.getAddressDetail())
                .setAddressId(addr.getAddressId())
                .setIsDefault(addr.getIsDefault())
                .setStreetName(areaInfo.getStreetName())
                .setStreetId(areaInfo.getStreetId())
                .setRegionName(areaInfo.getRegionName())
                .setRegionId(areaInfo.getRegionId())
                .setCityId(areaInfo.getCityId())
                .setCityName(areaInfo.getCityName())
                .setProvinceId(areaInfo.getProvinceId())
                .setProvinceName(areaInfo.getProvinceName())
                .setPhone(addr.getPhone())
                .setConsignee(addr.getConsignee());
        return shippingDTO;
    }

    @Override
    public void deleteAddrByClientIdAndId(Integer addrId, Integer clientId) {
        shippingMapper.deleteById(addrId,clientId);
    }

}
